// pages/my/orderDetail/index.js
const {
  baseURL,
  ip
} = require('../../../fetch/http')
const api = require('../../../fetch/api')
const app = getApp()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    ip: ip,
    id: '',
    detail: {}
  },
  clipBoard() {
    wx.setClipboardData({
      data: this.data.detail.expressNo,
    })
  },
  goodsDetail() {
    wx.navigateTo({
      url: '/pages/home/goodsDetail/index?id=' + this.data.detail.goodsId,
    })
  },
  async clearOrder() {
    Dialog.confirm({
        title: '您确认要取消订单吗？',
        message: '点击确认继续',
      })
      .then(() => {
        // on confirm
        api.clearOrder({
          order_id: this.data.order_id
        }).then(res => {
          if (res.code == 200) {
            app.toast('取消成功', 'success', () => {
              wx.navigateBack()
            })
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  async pay() {
    let res = await api.pay({
      order_id: this.data.order_id
    })
    if (res.code == 200) {
      app.toast('支付成功', 'success', () => {
        this.orderDetail()
      })
    }
  },
  async sign() {
    let res = await api.sign({
      order_id: this.data.order_id
    })
    if (res.code == 200) {
      app.toast('签收成功', 'success', () => {
        this.orderDetail()
      })
    }
  },
  async orderDetail() {
    let res = await api.orderDetail({
      order_id: this.data.order_id
    })
    this.setData({
      detail: res.data
    })
    // console.log(res)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.userId) {
      wx.setStorageSync('recommend', options.userId)
    }

    this.setData({
      order_id: options.id
    })
    this.orderDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    let userId = wx.getStorageSync('id')
    return {
      imageUrl: this.data.ip + '/static/wx/img/share.png',
      title: '金盾二茅',
      path: `/pages/home/goodsDetail/index?userId=${userId}&id=${this.data.id}`
    }
  }
})